"""Build go-spdk bindings C tests"""
from __future__ import print_function
import daos_build
import os

def scons():
    """Execute build"""
    Import('senv', 'prereqs', 'control_tgts')

    unit_env = senv.Clone()
    prereqs.require(unit_env, 'pmdk', 'spdk', 'isal', 'hwloc', 'cmocka')

    unit_env.AppendUnique(OBJPREFIX='c_')

    spdk_src = prereqs.get_src_path("spdk")
    nvme_lib_src = os.path.join(spdk_src, 'lib', 'nvme')
    spdk_incl_src = os.path.join(spdk_src, 'include')

    # spdk/lib/nvme to expose normally opaque types during tests
    unit_env.AppendUnique(CPPPATH=[nvme_lib_src, spdk_incl_src])

    # Explicitly link RTE & SPDK libs for CGO access
    libs = ['spdk_env_dpdk', 'spdk_nvme', 'spdk_vmd', 'rte_mempool']
    libs += ['rte_mempool_ring', 'rte_bus_pci', 'nvme_control']

    # Other libs
    libs += ['numa', 'dl', 'isal', 'cmocka', 'pthread']

    if GetOption('help'):
        return
    config = Configure(unit_env)

    look_path = os.path.join(nvme_lib_src, 'nvme_internal.h')
    if os.path.isfile(look_path):
        testbin = daos_build.test(unit_env, 'nvme_control_ctests',
                                  ['nvme_control_ut.c', unit_env.ncc,
                                   unit_env.nc] + control_tgts,
                                  LIBS=libs)
        unit_env.Install("$PREFIX/bin", testbin)
    else:
        print(look_path, " missing, skipping nvme_control_ut build")

    config.Finish()

if __name__ == "SCons.Script":
    scons()
